package dao.impl;

import cqcet.aibd.soft.ObjectUtil;
import dao.AddressDao;
import model.Address;
import model.User;

import java.util.List;

/**
 * @author yhl
 * @description 类说明
 * @date 2023-11-15 11:08
 * @email 2919886673@qq.com
 */
public class AddressDaoImpl implements AddressDao {

    @Override
    public List<Address> getAddressListByUserId(String id) {
        String sql = "select * from address where userId = ?";
        return new ObjectUtil<Address>().getList(sql, Address.class, id);
    }

    @Override
    public int addAddress(Address address) {
        String sql = "insert into address (id, userId, address, createTime) values (?,?,?,?)";
        return new ObjectUtil<Address>().add(sql, address.getId(), address.getUserId(), address.getAddress(), address.getCreateTime());
    }

    @Override
    public List<Address> checkDefaultAddressIsExist(User user) {
        String sql = "select id from address where userId = ? and defaultStatus = '1'";
        return new ObjectUtil<Address>().getList(sql, Address.class, user.getId());
    }

    @Override
    public int updateAddress(Address address) {
        String sql = "update address set address = ? , defaultStatus = ? where id = ?";
        return new ObjectUtil<Address>().update(sql, address.getAddress(), address.getDefaultStatus(), address.getId());
    }

    @Override
    public int updateOldAddressDefaultStatus(User user) {
        String sql = "update address set defaultStatus = '2' where userId = ? and defaultStatus = '1'";
        return new ObjectUtil<Address>().update(sql, user.getId());
    }

    @Override
    public int deleteAddressById(Address address) {
        String sql = "delete from address where id = ?";
        return new ObjectUtil<Address>().delete(sql, address.getId());
    }

    @Override
    public Address getUserDefaultAddress(User user) {
        String sql = "select * from address where userId = ? and defaultStatus = '1'";
        return new ObjectUtil<Address>().getOne(sql, Address.class, user.getId());
    }


}
